﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.NetworkRigidbody2D
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using UnityEngine;

#nullable disable
namespace Netick.Unity;

/// <summary>
/// This component is used to replicate the position and rotation of an object. Therefore, it’s the same as the Network Transform, the difference is that Network Rigidbody 2D must be used if you want to have client-side prediction over Box2D objects since it replicates several other properties necessary for correct prediction.
/// </summary>
[IgnoreCodeGen]
[AddComponentMenu("Netick/Network Rigidbody2D")]
[RequireComponent(typeof (Rigidbody2D))]
[DisallowMultipleComponent]
public sealed class NetworkRigidbody2D : NetworkTransform, IPhysicsComponent
{
  private Rigidbody2D _rigid;
  internal int _syncToOnlyInputSource;

  internal unsafe Vector2 NetworkedVelocity
  {
    get => NetickUnityUtils.GetVector2(this.StatePtr + 8, this._posPrecision);
    set => NetickUnityUtils.SetVector2(this.StatePtr + 8, value, this._posInversePrecision);
  }

  internal unsafe float NetworkedAngularVelocity
  {
    get => NetickUnityUtils.GetFloat(this.StatePtr + 10, this._posPrecision);
    set => NetickUnityUtils.SetFloat(this.StatePtr + 10, value, this._posInversePrecision);
  }

  internal unsafe bool NetworkedIsKinematic
  {
    get => this.StatePtr[11] == 1;
    set
    {
      int num1 = this.StatePtr[11];
      int num2 = value ? 1 : 0;
      this.StatePtr[11] = num2;
      if (!this.IsServer || num1 == num2)
        return;
      this.Entity.MarkAltered(this.StatePtr + 11);
    }
  }

  public override int InternalGetStateSizeWords() => 13;

  public override unsafe void InternalReset()
  {
    base.InternalReset();
    this._rigid = this.GetComponent<Rigidbody2D>();
    this.NetworkedVelocity = this._rigid.velocity;
    this.NetworkedAngularVelocity = this._rigid.angularVelocity;
    this.NetworkedIsKinematic = this._rigid.isKinematic;
    if (!this.Sandbox.IsServer)
      return;
    for (int index = 8; index < 4; ++index)
      this.Entity.MarkAltered(this.StatePtr + index);
  }

  public override void NetworkStart()
  {
    base.NetworkStart();
    this._syncToOnlyInputSource = this.Object.PredictionMode == Relevancy.InputSource ? 1 : 0;
  }

  public override void NetworkFixedUpdate()
  {
    base.NetworkFixedUpdate();
    if (!this.IsClient)
      return;
    if (this.Object.PredictionMode == Relevancy.InputSource)
    {
      if (this.IsInputSource)
      {
        this._rigid.isKinematic = this.NetworkedIsKinematic;
      }
      else
      {
        if (this._rigid.isKinematic)
          return;
        this._rigid.isKinematic = true;
      }
    }
    else
      this._rigid.isKinematic = this.NetworkedIsKinematic;
  }

  public override void NetcodeIntoGameEngine()
  {
    base.NetcodeIntoGameEngine();
    if (this._syncPosition)
      this._rigid.position = (Vector2) this.transform.position;
    if (this._syncRot)
      this._rigid.rotation = this.transform.eulerAngles.z;
    if (this.IsClient)
    {
      if (this.Object.PredictionMode == Relevancy.InputSource)
      {
        if (this.IsInputSource)
          this._rigid.isKinematic = this.NetworkedIsKinematic;
        else if (!this._rigid.isKinematic && !this.IsInputSource)
          this._rigid.isKinematic = true;
      }
      else
        this._rigid.isKinematic = this.NetworkedIsKinematic;
    }
    if (this._rigid.isKinematic || (double) (this.NetworkedVelocity - this._rigid.velocity).sqrMagnitude <= 9.9999997473787516E-06 && (double) this.NetworkedAngularVelocity - (double) this._rigid.angularVelocity <= 9.9999997473787516E-06)
      return;
    this._rigid.velocity = this.NetworkedVelocity;
    this._rigid.angularVelocity = this.NetworkedAngularVelocity;
  }

  public override unsafe void GameEngineIntoNetcode()
  {
    base.GameEngineIntoNetcode();
    this.NetworkedIsKinematic = this._rigid.isKinematic;
    int* numPtr = (int*) &new TransformBuffer();
    NetickUnityUtils.SetVector2(numPtr, this._rigid.velocity, this._posInversePrecision);
    this.Entity.SetState(numPtr, this.StatePtr + 8, 2, 0, this._syncToOnlyInputSource);
    NetickUnityUtils.SetFloat(numPtr, this._rigid.angularVelocity, this._posInversePrecision);
    this.Entity.SetState(numPtr, this.StatePtr + 10, 1, 0, this._syncToOnlyInputSource);
  }
}
